﻿using ControllerLayer;
using ModelLayer;
using System;
using WcfBetterPlace.WcfData;

namespace WcfBetterPlace
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name
    //       "BetterClientService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select
    //       BetterClientService.svc or BetterClientService.svc.cs at the Solution Explorer and
    //       start debugging.
    public class BetterClientService : IBetterClientService
    {
        public void DoWork()
        {
        }

        public PersonData GetCustomer(long id)
        {
            BetterAdminService adminService = new BetterAdminService();
            return adminService.GetCustomer(id);
        }

        public IntData UpdateCustomer(Customer customer)
        {
            BetterAdminService adminService = new BetterAdminService();
            return adminService.UpdateCustomer(customer);
        }

        public PersonList GetAllCustomers()
        {
            BetterAdminService adminService = new BetterAdminService();
            return adminService.GetAllCustomers();
        }

        public ReservationData CreateReservationUsingDijkstras(long startStationId, long endStationId, long customerId, DateTime date)
        {
            ReservationCtr resvCtr = new ReservationCtr();
            StationCtr stationCtr = new StationCtr();
            PersonCtr personCtr = new PersonCtr();
            ReservationData serviceData = new ReservationData();
            try
            {
                serviceData.SerData = resvCtr.CreateReservationUsingDijkstras(stationCtr.GetStation(startStationId), stationCtr.GetStation(endStationId), personCtr.GetCustomer(customerId), date);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not generate route with Dijkstra's.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public ReservationData CreateReservationUsingAStar(long startStationId, long endStationId, long customerId, DateTime date)
        {
            ReservationCtr resvCtr = new ReservationCtr();
            StationCtr stationCtr = new StationCtr();
            PersonCtr personCtr = new PersonCtr();
            ReservationData serviceData = new ReservationData();
            try
            {
                serviceData.SerData = resvCtr.CreateReservationUsingAStar(stationCtr.GetStation(startStationId), stationCtr.GetStation(endStationId), personCtr.GetCustomer(customerId), date);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not generate route with AStar.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public ReservationData GetReservation(long id)
        {
            ReservationCtr reservationCtr = new ReservationCtr();
            ReservationData serviceData = new ReservationData();
            try
            {
                serviceData.SerData = reservationCtr.GetReservation(id);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not get the reservation.\n" + ex.Message;
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public ReservationList GetAllReservations()
        {
            BetterAdminService adminService = new BetterAdminService();
            return adminService.GetAllReservations();
        }

        public ReservationList GetAllReservationsFromCustomer(long customerId)
        {
            ReservationCtr reservationCtr = new ReservationCtr();
            ReservationList serviceData = new ReservationList();
            try
            {
                serviceData.SerData = reservationCtr.GetAllReservationsFromCustomer(customerId);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not get the reservations for the customer.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public IntData UpdateReservation(Reservation resv)
        {
            var reservationCtr = new ReservationCtr();
            var serviceData = new IntData();
            try
            {
                serviceData.SerData = reservationCtr.UpdateReservation(resv);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not update the reservation.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public IntData CancelReservation(long resvId)
        {
            ReservationCtr reservationCtr = new ReservationCtr();
            IntData serviceData = new IntData();
            try
            {
                serviceData.SerData = reservationCtr.CancelReservation(reservationCtr.GetReservation(resvId));
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not renove the reservation.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public StationList GetAllStations()
        {
            BetterAdminService adminService = new BetterAdminService();
            return adminService.GetAllStations();
        }
    }
}